package pe.edu.upc.e41a.javasoft.domain;

import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@Table(name = "aviso")
/*
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "aviso")
@RooDbManaged(automaticallyDelete = true)
*/
public class Aviso {

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "codigo", columnDefinition = "INT")
    private Integer codigo;

	public Integer getCodigo() {
        return this.codigo;
    }

	public void setCodigo(Integer id) {
        this.codigo = id;
    }

	@ManyToOne
    @JoinColumn(name = "usuario", referencedColumnName = "codigo")
    private Usuario usuario;

	@Column(name = "titulo", columnDefinition = "VARCHAR", length = 20)
    @NotNull
    private String titulo;

	@Column(name = "mensaje", columnDefinition = "VARCHAR", length = 100)
    @NotNull
    private String mensaje;

	@Column(name = "posicion", columnDefinition = "INT")
    @NotNull
    private Integer posicion;

	@Column(name = "fecha_inicio", columnDefinition = "DATETIME")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date fechaInicio;

	@Column(name = "fecha_fin", columnDefinition = "DATETIME")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date fechaFin;

	public Usuario getUsuario() {
        return usuario;
    }

	public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }

	public String getTitulo() {
        return titulo;
    }

	public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

	public String getMensaje() {
        return mensaje;
    }

	public void setMensaje(String mensaje) {
        this.mensaje = mensaje;
    }

	public Integer getPosicion() {
        return posicion;
    }

	public void setPosicion(Integer posicion) {
        this.posicion = posicion;
    }

	public Date getFechaInicio() {
        return fechaInicio;
    }

	public void setFechaInicio(Date fechaInicio) {
        this.fechaInicio = fechaInicio;
    }

	public Date getFechaFin() {
        return fechaFin;
    }

	public void setFechaFin(Date fechaFin) {
        this.fechaFin = fechaFin;
    }
/*
	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Aviso().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countAvisoes() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Aviso o", Long.class).getSingleResult();
    }

	public static List<Aviso> findAllAvisoes() {
        return entityManager().createQuery("SELECT o FROM Aviso o", Aviso.class).getResultList();
    }

	public static Aviso findAviso(Integer codigo) {
        if (codigo == null) return null;
        return entityManager().find(Aviso.class, codigo);
    }

	public static List<Aviso> findAvisoEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Aviso o", Aviso.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Aviso attached = Aviso.findAviso(this.codigo);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Aviso merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Aviso merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
    */
}
